(function($){
	
	/** 在jQuery全局对象中扩展一个ibs命名空间 */
	$.extend({ibs: {
		initAddress: function(options){
			if (options != undefined && options.region == undefined && options.province != "") {
				options["region"] = options.province;
			}
			
			var provinceDefault = "comProvince";
			var defaults = {
				render: 'render',
				region: provinceDefault,
				province: provinceDefault,
				city: 'comCity',
				area: 'comArea',
				parentId: '1'
			};
			var opt = $.extend({}, defaults, options);
			
			var selectStr = "<select name=" + opt.region + " id= " + opt.region + " style='float:left'><option value=''>全部</option>";
			var path = $("#path_hid").val();
			$.ajax({
				async: false,
				url: path + "/operation/region/provinceList.htm",
				data: 'parentId=' + opt.parentId,
				type: 'post',
				dataType: 'json',
				success: function(data){
					$(data).each(function(i, n){
						selectStr += "<option value=" + n.id + ">" + n.regionName +"</option>";
					});
					selectStr += "</select>";
				},
				complete: function(){},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert("发生异常！" + XMLHttpRequest.readyState + textStatus + errorThrown);
				}
			});

			var is = $("#" + opt.render).next().is("select") ;
			if (is) {
				$("#" + opt.render).next().remove();
			}
			$("#" + opt.render).after(selectStr);
			
			$("#" + opt.region).change(function(){
				var parentId = $("#" + opt.region).val();
				if (opt.region == opt.province) {
					if ($("#" + opt.province).next().is("select") && $("#" + opt.province).next().next().is("select")) {
						$("#" + opt.province).next().next().remove();
					}
					$.ibs.initAddress({"parentId": parentId, "region": opt.city, "province":opt.province, "city":opt.city, "area": opt.area, "render": opt.province});
				} else if (opt.region == opt.city) {
					$.ibs.initAddress({"parentId": parentId, "region": opt.area, "province":opt.province, "city":opt.city, "area": opt.area, "render": opt.city});
				} ;
			});
			
			/**
			if (opt.render != "preAppend") {
				$("#" + opt.render).next().remove();
			}
			*/
			// return $("#" + opt.render);
			// return $(this).next();  
			
		}
	}});

	/**将方法扩展到jQuery的原型*/
	/**
	$.extend($.fn, {
		initAddress: function() {alert("hello");}
	});
	*/

	/**
	$.fn.initAddress = function(options){
		// 同上面 initAddress 方法中代码
	};
	*/
})(jQuery);
